﻿
#include "RouteViewController.h"

static string routeTypeStr[3] =
{
	UTF8("驾车"),
	UTF8("公交"),
	UTF8("步行")
};

RouteViewController::RouteViewController() : m_iCode(0), m_iIndex(1)
{

}

RouteViewController::~RouteViewController()
{

}

void RouteViewController::viewDidLoad()
{
    // Do any additional setup after loading the view from its nib.

	this->setNavigationBarItem(CANavigationBarItem::create(UTF8("路线规划")));

	const DSize& viewSize = this->getView()->getFrame().size;

	CASegmentedControl* segment = CASegmentedControl::createWithFrame(DRect(20, 15, viewSize.width - 20 * 2, 70), 3);
	for (int i = 0; i < 3; ++i)
	{
		segment->setTitleForSegmentAtIndex(routeTypeStr[i], i);
		
	}
	segment->addTarget(this, CASegmentedControl_selector(RouteViewController::segController));
	segment->setTitleFontSize(30);
	segment->setTitleColor(CAColor_black);
	segment->setTitleSelectedColor(CAColor_white);
	this->getView()->addSubview(segment);

	m_pAmapView = AmapView::createWithFrame(DRect(0, 100, viewSize.width, viewSize.height * 0.5 - 100));
	m_pAmapView->setAmapViewDelegate(this);
	this->getView()->addSubview(m_pAmapView);	

	m_pAmapView->setAmapRouteViewType(39.942295, 116.335891, 39.995576, 116.481288, DriveRoute_Type);
	m_pAmapView->searchRoute();

	m_pTableView = CATableView::createWithFrame(DRect(0, viewSize.height * 0.5, viewSize.width, viewSize.height * 0.5));
	m_pTableView->setTableViewDataSource(this);
	m_pTableView->setTableViewDelegate(this);
	m_pTableView->setSeparatorColor(CAColor_black);
	m_pTableView->setSeparatorViewHeight(1.2f);
	m_pTableView->setAllowsSelection(true);
	this->getView()->addSubview(m_pTableView);
}

void RouteViewController::viewDidUnload()
{
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

void RouteViewController::onDriveRouteSearched(int code, DriveRouteData data)
{
	m_iCode = code;
	drivateData = data;

	CAScheduler::schedule(schedule_selector(RouteViewController::updateUI), this, 0);
}

void RouteViewController::onWalkRouteSearched(int code, WalkRouteData data)
{
	m_iCode = code;
	walkData = data;

	CAScheduler::schedule(schedule_selector(RouteViewController::updateUI), this, 0);
}

void RouteViewController::onBusRouteSearched(int code, std::vector<BusRouteData> data)
{
	m_iCode = code;
	busData = data;

	CAScheduler::schedule(schedule_selector(RouteViewController::updateUI), this, 0);
}

void RouteViewController::onBusRouteSteps(int code, int bustag, std::vector<BusRouteStep> datas)
{
	CCLog("--------------bustag begin:%d---------------------", bustag);
	CCLog("--------------bustag size:%d---------------------", (int)datas.size());
	for (int i = 0; i < (int)datas.size(); ++i)
	{
		string str = crossapp_format_string(UTF8("%d、类型：%d 名称：%s 距离：%.2f 时间：%.2f 路过站：%s"), i + 1, datas[i].type, datas[i].name.c_str(), datas[i].distance, datas[i].duration, datas[i].stations.c_str());
		CCLog("%s", str.c_str());
	}
	CCLog("--------------bustag end:%d------------------------", bustag);
}

CATableViewCell* RouteViewController::tableCellAtIndex(CATableView* table, const DSize& cellSize, unsigned int section, unsigned int row)
{
	CATableViewCell* cell = (CATableViewCell*)table->dequeueReusableCellWithIdentifier("Cell");
	if (!cell)
	{
		cell = CATableViewCell::create("Cell");
		cell->setFrame(DRect(0, 0, cellSize.width, cellSize.height));

		CALabel* lb = CALabel::createWithFrame(DRect());
		lb->setFontSize(30);
		lb->setTextAlignment(CATextAlignmentLeft);
		lb->setVerticalTextAlignmet(CAVerticalTextAlignmentCenter);
		lb->setTag(100);
		cell->addSubview(lb);
	}

	string lbstr;
	if (m_iIndex == DriveRoute_Type)
	{
		RouteStep* step = &drivateData.v_steps[row];

		lbstr = crossapp_format_string(UTF8("说明：%s 方向：%s\n距离：%.2fm 时间：%.2fs"), step->instruct.c_str(), step->orientation.c_str(), step->distance, step->duration);
	}
	else if (m_iIndex == BusRoute_Type)
	{
		BusRouteData* bus = &busData[row];

		lbstr = crossapp_format_string(UTF8("说明：%s 描述：%s\n距离：%dm 时间：%ds 花费：%.2f"), bus->pathTitle.c_str(), bus->pathDes.c_str(), bus->distance, bus->duration, bus->cost);
	}
	else if (m_iIndex == WalkRoute_Type)
	{
		RouteStep* step = &walkData.v_steps[row];

		lbstr = crossapp_format_string(UTF8("说明：%s 方向：%s\n距离：%.2fm 时间：%.2fs"), step->instruct.c_str(), step->orientation.c_str(), step->distance, step->duration);
	}
	
	CALabel* lb = (CALabel*)cell->getSubviewByTag(100);
	lb->setFrame(DRect(20, 0, cellSize.width - 20 * 2, cellSize.height));
	lb->setText(lbstr);

	return cell;
}

unsigned int RouteViewController::tableViewHeightForRowAtIndexPath(CATableView* table, unsigned int section, unsigned int row)
{
	return 100;
}

unsigned int RouteViewController::numberOfRowsInSection(CATableView* table, unsigned int section)
{
	if (m_iIndex == DriveRoute_Type)
	{
		return (unsigned int)drivateData.v_steps.size();
	}
	else if (m_iIndex == BusRoute_Type)
	{
		return (unsigned int)busData.size();
	}
	else if (m_iIndex == WalkRoute_Type)
	{
		return (unsigned int)walkData.v_steps.size();
	}

	return 0;
}

CAView* RouteViewController::tableViewSectionViewForHeaderInSection(CATableView* table, const DSize& viewSize, unsigned int section)
{
	CAView* headView = CAView::createWithFrame(DRect(0, 0, viewSize.width, viewSize.height));

	string lbstr;
	if (m_iIndex == DriveRoute_Type)
	{
		lbstr = crossapp_format_string(UTF8("m_iCode:%d 总时间：%ds 总距离：%dm 描述：%s 打车：%.2f元"), m_iCode, drivateData.duration, drivateData.distance, drivateData.desctription.c_str(), drivateData.taxiCost);
	}
	else if (m_iIndex == BusRoute_Type)
	{
		lbstr = crossapp_format_string(UTF8("m_iCode:%d 提供线路：%d"), m_iCode, (int)busData.size());
	}
	else if (m_iIndex == WalkRoute_Type)
	{
		lbstr = crossapp_format_string(UTF8("m_iCode:%d 总时间：%ds 总距离：%dm 描述：%s"), m_iCode, walkData.duration, walkData.distance, walkData.description.c_str());
	}

	CALabel* lb = CALabel::createWithFrame(DRect(20, 0, viewSize.width - 20 * 2, viewSize.height));
	lb->setFontSize(30);
	lb->setTextAlignment(CATextAlignmentLeft);
	lb->setVerticalTextAlignmet(CAVerticalTextAlignmentCenter);
	lb->setText(lbstr);
	headView->addSubview(lb);

	return headView;
}

unsigned int RouteViewController::tableViewHeightForHeaderInSection(CATableView* table, unsigned int section)
{
	return 120;
}

void RouteViewController::tableViewDidSelectRowAtIndexPath(CATableView* table, unsigned int section, unsigned int row)
{
	if (m_iIndex == BusRoute_Type)
	{
		m_pAmapView->showBusRouteView(busData[row].tag);
		m_pAmapView->getBusRouteStep(busData[row].tag);
	}
}

void RouteViewController::updateUI(float t)
{
	CAScheduler::unschedule(schedule_selector(RouteViewController::updateUI), this);

	m_pTableView->reloadData();
}

void RouteViewController::segController(CASegmentedControl* seg, int index)
{
	m_iIndex = index + 1;

	m_pTableView->reloadData();

	m_pAmapView->setAmapRouteViewType(39.942295, 116.335891, 39.995576, 116.481288, (AmapRouteViewType)(index + 1));
	if (m_iIndex == BusRoute_Type)
	{
		m_pAmapView->setBusRouteCity(UTF8("北京"));
	}
	m_pAmapView->searchRoute();
}
